﻿Imports System.Text
Imports System.Net
Imports System.IO

Public Class RequestSender

    Private requestType As String
    Private Server As String
    Private World As String


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="requestType">The Post location of the request</param>
    ''' <remarks></remarks>

    Public Sub New(ByVal requestType As String, ByVal Server As String, ByVal World As String)
        Me.Server = Server
        Me.World = World
        Me.requestType = "http://" & Server & World & requestType
    End Sub

    ''' <summary>
    ''' Makes the headers for the post request.  This is needed for all queries.
    ''' </summary>
    ''' <returns>The request to be sent</returns>
    ''' <remarks></remarks>
    Private Function makeHeaders(ByVal request As String) As HttpWebRequest
        ' Create a new 'HttpWebRequest' Object to the mentioned URL.
        System.Net.ServicePointManager.Expect100Continue = False
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(requestType), HttpWebRequest)

        myHttpWebRequest.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = request
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentLength property of the WebRequest.
        myHttpWebRequest.Host = Server
        myHttpWebRequest.KeepAlive = True
        myHttpWebRequest.Referer = "http://" & Server & World & "/index.aspx"
        myHttpWebRequest.ContentLength = byteArray.Length
        myHttpWebRequest.Headers.Add("Origin: " & "http://" & Server)
        myHttpWebRequest.UserAgent = " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24"
        myHttpWebRequest.ContentType = "application/json"
        myHttpWebRequest.Headers.Add("X-Qooxdoo-Response-Type: application/json")
        myHttpWebRequest.Accept = "*/*"
        myHttpWebRequest.Headers.Add("Accept-Encoding: gzip,deflate,sdch")
        myHttpWebRequest.Headers.Add("Accept-Language: en-US,en;q=0.8")
        myHttpWebRequest.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3")
        ' Get the request stream.
        Dim dataStream As Stream = myHttpWebRequest.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        makeHeaders = myHttpWebRequest
    End Function

    ''' <summary>
    ''' This is a normal request. In all futur requests this class should be used.
    ''' </summary>
    ''' <returns>Json Object of the results of the request</returns>
    ''' <remarks></remarks>
    Public Function sendRequest(ByVal request As String) As String
        Dim i As Integer = 0
        Dim information As New StringBuilder("")
        Dim myHttpWebResponse As HttpWebResponse
        Dim myHttpWebRequest As HttpWebRequest


        ' Create a new 'HttpWebRequest' Object to the mentioned URL.
        myHttpWebRequest = makeHeaders(request)

        ' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
        Try
            myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        Catch ex As Exception
            Return ""
        End Try


        Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
        Dim streamRead As New StreamReader(streamResponse)
        Dim readBuff(256) As [Char]
        Dim count As Integer = streamRead.Read(readBuff, 0, 256)
        While count > 0
            Dim outputData As New [String](readBuff, 0, count)
            information.Append(outputData)
            count = streamRead.Read(readBuff, 0, 256)
        End While
        ' Close the Stream object.
        streamResponse.Close()
        streamRead.Close()
        ' Release the HttpWebResponse Resource.
        myHttpWebResponse.Close()

        Return information.ToString

    End Function

    ''' <summary>
    ''' Used to send purify requests
    ''' </summary>
    ''' <returns>True or false wether the request failed or not</returns>
    ''' <remarks></remarks>
    Public Function sendReserachRequest(ByVal request As String) As String
        Dim i As Integer = 0
        Dim information As New StringBuilder("")
        Dim myHttpWebResponse As HttpWebResponse
        Dim myHttpWebRequest As HttpWebRequest
        'Dim response As jsonobject
        Try
            ' Create a new 'HttpWebRequest' Object to the mentioned URL.
            myHttpWebRequest = makeHeaders(request)
        Catch ex As Exception
            Return "False"
        End Try

        ' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
        Try
            myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        Catch ex As Exception
            Return "False"
        End Try
        Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
        Dim streamRead As New StreamReader(streamResponse)
        Dim readBuff(256) As [Char]
        Dim count As Integer = streamRead.Read(readBuff, 0, 256)

        Dim outputData As New [String](readBuff, 0, count)
        information.AppendLine(outputData)
        ' Close the Stream object.
        streamResponse.Close()
        streamRead.Close()
        ' Release the HttpWebResponse Resource.
        myHttpWebResponse.Close()

        sendReserachRequest = (outputData)
    End Function
End Class
